package com.js.admin.controller;

import com.js.admin.constant.AdminConstant;
import com.js.admin.entity.BaseMenu;
import com.js.admin.service.BaseMenuService;
import com.js.admin.vo.MenuTree;
import com.js.auth.client.annotation.CheckUserToken;
import com.js.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ygl
 * @create 2018-11-02
 * @DESCRIPTION
 **/
@CheckUserToken
@Api(tags="菜单模块")
@RestController
@RequestMapping("menu")
public class BaseMenuController extends BaseController<BaseMenu,String> {


    @Autowired
    private BaseMenuService baseMenuService;

    @ApiOperation("获取菜单列表")
    @GetMapping(value = "/list")
    public List<BaseMenu> list(String title) {
        Example example = new Example(BaseMenu.class);
        if (StringUtils.isNotBlank(title)) {
            example.createCriteria().andLike("title", "%" + title + "%");
        }
        return baseMenuService.selectByExample(example);
    }

    @ApiOperation("获取菜单树")
    @GetMapping(value = "/tree")
    public List<MenuTree> getTree(String title) {
        Example example = new Example(BaseMenu.class);
        if (StringUtils.isNotBlank(title)) {
            example.createCriteria().andLike("title", "%" + title + "%");
        }
        return MenuTree.buildTree(baseMenuService.selectByExample(example), AdminConstant.ROOT);
    }

}
